Completed
Push — master ( 492ecf...92d982 )
by Patrick
07:35
created

user_edit.js ➔ populateUserDropdown   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 13
rs 9.4285
1
var _uid = null;
2
3
function getUID()
4
{
5
    if(_uid != null)
6
    {
7
        return _uid;
8
    }
9
    else
10
    {
11
        return getParameterByName('uid');
12
    }
13
}
14
15
var leads = null;
16
var user = null;
17
18
function areasDone(jqXHR)
19
{
20
    if(jqXHR.status !== 200)
21
    {
22
        alert('Unable to obtain area list!');
23
        console.log(jqXHR);
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
24
        return;
25
    }
26
    var areas = jqXHR.responseJSON;
27
    for(i = 0; i < areas.length; i++)
0 ignored issues
show
Bug introduced by
The variable i seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.i.
Loading history...
28
    {
29
        $('#ou').append('<option value="'+areas[i].short_name+'">'+areas[i].name+'</option>');
30
        $.ajax({
31
            url: '../api/v1/areas/'+areas[i].short_name+'/leads',
32
            type: 'get',
33
            dataType: 'json',
34
            context: areas[i].short_name,
35
            success: leadsDone});
36
    }
37
    if(user != null)
38
    {
39
        $('#ou').val(user.ou);
40
        area_change($('#ou'));
41
    }
42
}
43
44
function leadsDone(data)
45
{
46
    if(leads === null)
47
    {
48
        leads = {};
49
    }
50
    leads[this] = data;
51
    area_change($('#ou'));
52
}
53
54
function area_change(control)
55
{
56
    var val = $(control).val();
57
    if(val == '')
58
    {
59
        return;
60
    }
61
    if(leads != null)
62
    {
63
        $('#title').html('<option></option>');
64
        var areaLeads = leads[val];
65
        if(areaLeads === undefined) return;
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
66
        for(i = 0; i < areaLeads.length; i++)
0 ignored issues
show
Bug introduced by
The variable i seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.i.
Loading history...
67
        {
68
            var option = $('<option value="'+areaLeads[i].short_name+'">'+areaLeads[i].name+'</option>');
69
            if(user !== null && user.title[0] == areaLeads[i].short_name)
70
            {
71
                option.attr('selected', 'true');
72
            }
73
            $('#title').append(option);
74
        }
75
    }
76
}
77
78
function userDataDone(jqXHR)
79
{
80
    if(jqXHR.status !== 200)
81
    {
82
        alert('Unable to obtain user data!');
83
        console.log(jqXHR);
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
84
        return;
85
    }
86
    user = jqXHR.responseJSON;
87
    $('#uid').html(user.uid);
88
    $('#uid_x').val(user.uid);
89
    $('#old_uid').val(user.uid);
90
    $('#dn').html(user.dn);
91
    $('#givenName').val(user.givenName);
92
    $('#sn').val(user.sn);
93
    $('#displayName').val(user.displayName);
94
    $('#mail').val(user.mail);
95
    $('#mobile').val(user.mobile);
96
    $('#postalAddress').val(user.postalAddress);
97
    $('#postalCode').val(user.postalCode);
98
    $('#l').val(user.l);
99
    $('#st').val(user.st);
100
    $('#ou').val(user.ou);
101
    area_change($('#ou'));
102
    $('#title').val(user.title[0]);
103
    $('#user_data').show(); 
104
}
105
106
function populateAreaDropdown()
107
{
108
    $.when(
109
        $.ajax({
110
            url: '../api/v1/areas',
111
            type: 'get',
112
            dataType: 'json',
113
            complete: areasDone})
114
    ).done(populateUserData);
115
}
116
117
function populateUserData()
118
{
119
    var uid = getUID();
120
    if(($('#user_data').length > 0) && (uid != null))
121
    {
122
        $.ajax({
123
            url: '../api/v1/users/'+uid,
124
            type: 'get',
125
            dataType: 'json',
126
            complete: userDataDone});
127
    }
128
}
129
130
function userSubmitDone(jqXHR)
131
{
132
    if(jqXHR.status !== 200)
133
    {
134
        alert('Unable to set user data!');
135
        console.log(jqXHR);
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
136
        return;
137
    }
138
    alert("Success!");
139
    location = 'user_edit.php?uid='+getUID();
140
}
141
142
function userDataSubmitted(e)
143
{
144
    e.preventDefault();
145
    var obj = $(e.target).serializeObject();
146
    $.ajax({
147
        url: '../api/v1/users/'+getUID(),
148
        data: JSON.stringify(obj),
149
        contentType: 'application/json',
150
        type: 'PATCH',
151
        dataType: 'json',
152
        processData: false,
153
        complete: userSubmitDone});
154
    return false;
155
}
156
157
function do_user_edit_init()
158
{
159
    populateAreaDropdown();
160
    $("#form").submit(userDataSubmitted);
161
}
162
163
$(do_user_edit_init);
164